<?php

Yii::import('common.models._base.BaseUsers');

class Users extends BaseUsers {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public $confirm_password;

    public function rules() {
        return array(
            array('username,password', 'required'),
            array('username', 'unique', 'on' => 'checkout', 'message' => Yii::t('validation', 'username has already been taken.')),
            array('username', 'unique', 'on' => 'insert'),
            array('username', 'email'),
            array('confirm_password', 'required', 'on' => 'insert'),
            array('password', 'length', 'min' => 4, 'on' => 'insert'),
            array('confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => Yii::t("Users", "Password and Confirm Password are not same."), 'on' => 'insert'),
            array('createtime, lastvisit, superuser, status', 'numerical', 'integerOnly' => true),
            array('username', 'length', 'max' => 50),
            array('password, activationKey', 'length', 'max' => 128),
            array('activationKey, createtime, lastvisit, superuser, status', 'default', 'setOnEmpty' => true, 'value' => null),
            array('id, username, password, activationKey, createtime, lastvisit, superuser, status, cache_last_updated', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels() {
        return array(
            'id' => Yii::t('app', 'ID'),
            'username' => Yii::t('app', 'Email'),
            'password' => Yii::t('app', 'Password'),
        );
    }

    public function findBySocial($provider, $identifier) {
        return $this->findByAttributes(array(
                    'social_provider' => $provider,
                    'social_identifier' => $identifier,
        ));
    }

    // for social user login
    public function findByAuthSocial($socialCode1, $socialCode2) {
        return $this->findByAttributes(array(
                    'social_info1' => $socialCode1,
                    'social_info2' => $socialCode2,
                    'social_secret_code' => hash('dfsdfdfsf'),
        ));
    }

    // for normal registered user login
    public function findByAuthUser($username, $password) {
        $user = $this->findByAttributes(array(
            'LOWER(email)' => strtolower($username),
        ));
        return $user->password === crypt($password, $user->password) ? $user : null;
    }

    public static function getFrontEndUser($userId) {
        $profileName = Profiles::model()->findByAttributes(array('user_id' => $userId));
        return $profileName;
    }

    public static function getBackEndUser($userId) {
        $profileName = UserProfile::model()->findByAttributes(array('user_id' => $userId));
        return $profileName;
    }

    public static function getTrainerProfileReview($userId) {
        $dataVal = TrainerProfileReview::model()->findByAttributes(array('to_user_id' => $userId));
        return ($dataVal != '') ? CHtml::link('View Review', Yii::app()->createUrl('trainerProfileReview/frontUserReview', array("id" => $userId))) : 'No Review';
    }

    public static function getTrainerbackProfileReview($userId) {
        $dataVal = TrainerProfileReview::model()->findByAttributes(array('backend_user_id' => $userId));
        return ($dataVal != '') ? CHtml::link('View Review', Yii::app()->createUrl('trainerProfileReview/backUserReview', array("id" => $userId))) : 'No Review';
    }

    public function activate($email, $activationKey) {
        $find = Users::model()->findByAttributes(array('username' => $email));
        if (!empty($find->status)) {
            return true;
        } elseif (!empty ($find->activationKey)&&$find->activationKey == $activationKey) {
            $find->activationKey = guid() . microtime();
            $find->status = 1;
            $find->save();
            return true;
        }
        else
            return false;
    }

  

}
